##############################################################################
##############################################################################
##############################################################################

library(lattice)
library(latticeExtra)
library(MASS)
library(foreign)

setwd("~/Dropbox/Perceived vs. Affective Polarization/Data and Code/Cumulative")

##############################################################################

####
## Figure 1
####

tertile <- read.csv("tertiles, pres years.csv")

pdf("overtime.pdf")

xyplot(sum ~ year | as.factor(panel), 
       data = tertile,
       aspect = 1,
       ylab = "Perceived Polarization            Affective Polarization",
       xlab = "",
       main = "",
       type = "l",
       col = "black",
       pch = 1,
       layout = c(3, 2),
       strip=strip.custom(var.name="", 
                          factor.levels=c("Affective: lower tertile",
                                          "Affective: middle tertile",
                                          "Affective: upper tertile",
                                          "Perceived: lower tertile",
                                          "Perceived: middle tertile",
                                          "Perceived: upper tertile"
                          )),
       panel = function(x,y, ...) {
         panel.xyplot(x, y, ...)
         panel.lmline(x, y, lty = 3, col="red")
       }
)

dev.off()

##############################################################################

####
## Figure 2
####

sophdata <- read.dta("polarization by info.dta")

text<-c("r=0.51", "r=0.60", "r=0.70")

pdf("infoplot.pdf")

xyplot(polav ~ year | as.factor(info3), 
       data = sophdata,
       aspect = 1,
       ylab = "Polarization",
       xlab = "",
       main = "",
       type = "l",
       col = "black",
       group = sophdata$poltype,
       lty = c(1, 2),
       layout = c(3, 1),
       pch = 1,
       strip=strip.custom(factor.levels=c("Low Info", "Mid Info", "High Info"
                          )),
       #scales=list(x = list(
        # cex = .6,
         #labels = c("'72", "'76", "'80", "'84", "'88",
          #          "'92", "'96", "'00", "'04", "'08",
           #         "'12", "'16"))),
       key=list(columns=1,lines=list(lty=c(1,2)), 
                text=list(c("Perceived", "Affective"))),
       panel = panel.superpose,
       panel.groups = function(x, y, subscripts, groups, col, ...) {
         panel.xyplot(x, y, lty =...)
         panel.text(x=1980, y=0.55, labels = text[panel.number()])
       }
)

dev.off()

##############################################################################

####
## Supplemental Appendix Analyses
####

# Trends in perceived over time
perceived <- read.csv("Perceived Over Time.csv", header = TRUE)
head(perceived)

pdf("perceived-indicators.pdf")

xyplot(mean ~ year | item,
       data = na.omit(perceived),
       aspect = 0.75,
       col = c("black"),
       pch = 16,
       type = "b",
       layout = c(3, 2),
       xlab = "Year",
       ylab = "Mean Difference"
       )

dev.off()

# Trends in affective over time
affective <- read.csv("Affective Over Time.csv", header = TRUE)
head(affective)

pdf("affective-indicators.pdf")

xyplot(mean ~ year | item,
       data = na.omit(affective),
       aspect = 0.75,
       col = c("black"),
       pch = 16,
       type = "b",
       layout = c(3, 1),
       xlab = "Year",
       ylab = "Mean Difference"
)

dev.off()

